﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace BLL
{
    public class QLPhanCung
    {
        public static IEnumerable<PhanCung> GetAll()
        {
            return Generic.GetAll<PhanCung>();
        }

        public static bool Insert(PhanCung pc)
        {
            return Generic.Insert<PhanCung>(pc);
        }

        public static int Delete(PhanCung pc)
        {
            return Generic.DeleteByCondition<PhanCung>(x => x.MaPC == pc.MaPC);
        }
        public static ActionResult DeleteByTenPC(PhanCung pc)
        {
            ActionResult kq = new ActionResult("xóa phần cứng");
            try
            {
                Generic.DeleteByCondition<PhanCung>(x => x.TenPhanCung == pc.TenPhanCung&&x.MaMT==pc.MaMT);
            }
            catch (Exception ex)
            {
                kq.Exceptions.Add(ex);
            }
            return kq;
        }
        public static int DeleteAll(IEnumerable<PhanCung> lpc)
        {
            return Generic.DeleteByCondition<PhanCung>(pc => lpc.Where(x => x.MaPC == pc.MaPC).Count() == 1); 
        }

        public static int Update(PhanCung pc)
        {

            return Generic.EditByCondition<PhanCung>(x => x.TenPhanCung == pc.TenPhanCung&&x.MaMT==pc.MaMT, x=>{
                x.TenPhanCung = pc.TenPhanCung;
                x.ThongTinPhanCung = pc.ThongTinPhanCung;
            });
        }

        public static IEnumerable<PhanCung> FindByCondition(Func<PhanCung, bool> func)
        {
            return Generic.FindByCondition<PhanCung>(func);
        }

        public static PhanCung FindByUserName(string name)
        {
            return Generic.FindByCondition<PhanCung>(x => x.TenPhanCung == name).First();
        }
        public static PhanCung FindByPhanCung(PhanCung pc)
        {
            return Generic.FindByCondition<PhanCung>(x => x.TenPhanCung == pc.TenPhanCung&&pc.MaMT==x.MaMT).First();
        }
        public static ValidationResult Validate(List<PhanCung> dsphancung)
        {
            ValidationResult kq = new ValidationResult();
            bool kiemtratrung = true;
          for (int i = 0; i < dsphancung.Count; i++)
          {
              for (int j = i; j < dsphancung.Count; j++)
              {
                  if (dsphancung[i] == dsphancung[j])
                  {
                      kiemtratrung = false;
                      break;
                  }

              }
          }
          if (kiemtratrung)
              kq.Messages.Add("trùng tên phần cứng");
            return kq;
        }
        public static ActionResult ThemDsPhanCung(List<PhanCung> dsphancung)
        {
            ActionResult kq = new ActionResult();
            kq.Exceptions.AddRange(Validate(dsphancung).Messages.Select(x=>new Exception(x)));
            try
            {
                dsphancung.ForEach(x => Generic.Insert<PhanCung>(x));
            }
            catch (Exception ex)
            {
                kq.Exceptions.Add(ex);
            }
            return kq;
        }
    }
}
